

const util = require('../../utils/util.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    showpopup:false,
    storesItem:{},
    storesItemValue:'',
    storeList:[],

    mindate: '',
    maxdate: '',
    username: '',
    mobile: '',
    eddDate: '',
    workPlace: '',
    workPlaceParticular: '',
    remark: '',

    selectIndex: '',
    selectLabel: '',
    list: [
      {
        name: '1人',
        id: 0
      },
      {
        name: '2人',
        id: 1
      },
    ],
    selectIndex2: '',
    selectLabel2: '',
    list2: [
      {
        name: '男',
        id: 0
      },
      {
        name: '女',
        id: 1
      },
    ],
    selectIndex21: '',
    selectLabel21: '',
    list21: [
      {
        name: '自理',
        id: 0
      },
      {
        name: '半自理',
        id: 1
      },
      {
        name: '全护理',
        id: 2
      },
    ],

    selectIndex3: '',
    selectLabel3: '',
    list3: [
      {
        name: '三口之家',
        id: 0
      },
      {
        name: '四口之家',
        id: 1
      },
      {
        name: '五口之家',
        id: 2
      },
      {
        name: '五口以上',
        id: 3
      },
    ],
    selectIndex4: '',
    selectLabel4: '',
    serviceTimeBeg: '',
    serviceTimeEnd: '',
    list4: [
      {
        name: '住家',
        id: 0
      },
      {
        name: '白班',
        id: 1
      }
    ],

    selectIndex5: '',
    selectLabel5: '',
    list5: [
      {
        name: '同宝妈一个房间',
        id: 0
      },
      {
        name: '有独立房间',
        id: 1
      },
      {
        name: '其他',
        id: 2
      }
    ],
    selectIndex6: '',
    selectLabel6: '',
    list6: [
      {
        name: '是',
        id: 0
      },
      {
        name: '否',
        id: 1
      }
    ],
    selectIndex61: '',
    selectLabel61: '',
    list61: [
      {
        name: '第一胎',
        id: 0
      },
      {
        name: '第二胎',
        id: 1
      },
      {
        name: '第三胎及以上',
        id: 2
      }
    ],

    selectIndex7: '',
    selectLabel7: '',
    list7: [
      {
        name: '单胎',
        id: 0
      },
      {
        name: '双胎',
        id: 1
      }
    ],

    selectIndex8: '',
    selectLabel8: '',
    list8: [
      {
        name: '宁波地区',
        id: 0
      },
      {
        name: '浙江省内',
        id: 1
      },
      {
        name: '湖北',
        id: 2
      },
      {
        name: '安徽',
        id: 3
      },
      {
        name: '河南',
        id: 4
      },
      {
        name: '江西',
        id: 5
      },
      {
        name: '不限',
        id: 6
      },
    ],

    selectIndex9: '',
    selectLabel9: '',
    list9: [
      {
        name: '金牌月嫂（服务8年以上）18800-21800元',
        id: 0
      },
      {
        name: '高级月嫂（服务5年以上）16800-18800元',
        id: 1
      },
      {
        name: '普通月嫂（服务2年以上）12800-13800元',
        id: 2
      }
    ],
    selectIndex10: '',
    selectLabel10: '',
    list10: [
      {
        name: '宁波地区',
        id: 0
      },
      {
        name: '浙江省内',
        id: 1
      },
      {
        name: '湖北',
        id: 2
      },
      {
        name: '安徽',
        id: 3
      },
      {
        name: '河南',
        id: 4
      },
      {
        name: '江西',
        id: 5
      },
      {
        name: '不限',
        id: 6
      },
    ],

    selectIndex10: '',
    selectLabel10: '',
    list10: [
      {
        name: '顺产',
        id: 0
      },
      {
        name: '刨宫产',
        id: 1
      }
    ],


    selectIndex12: '',
    selectLabel12: '',
    list12: [
      {
        name: '不是很急 15-20天',
        id: 0
      },
      {
        name: '比较急 5-7天',
        id: 1
      },
      {
        name: '非常急 3-5天',
        id: 2
      },
      {
        name: '特别急 1-3天',
        id: 3
      },
    ],
    showDate: false
  },
  onPopupClick(){
    this.setData({
      showpopup: true,
    })
  },
  onPopupClose(){
    this.setData({
      showpopup: false,
    })
  },
  doPopupItem(e){
    const data = e.currentTarget.dataset;
    this.setData({
      storesItem:data.item,
      showpopup:false,
      storesItemValue:data.item.address+'，'+data.item.name
    })
  },
  getStoresInit(){
    util.https('storesMgmt/storesQry', {
    }, (res) => {
      let storesBoList = res.storesBoList;
      this.setData({
        storeList:storesBoList
      })
    }, (res) => {
      wx.showModal({
        title: '温馨提示',
        content: res.errorMsg,
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
    })
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  getDate() {
    this.setData({
      showDate: true
    })
  },
  onCloseDate() {
    this.setData({
      showDate: false
    })
  },
  onConfirmDate(event) {
    this.setData({
      showDate: false,
      eddDate: this.formatDate(event.detail),
    });
  },

  clickItem(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex: data.index,
    })
  },
  clickItem2(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex2: data.index,
    })
  },
  clickItem21(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex21: data.index,
    })
  },

  clickItem3(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex3: data.index,
    })
  },

  clickItem4(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex4: data.index,
    })
  },
  clickItem5(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex5: data.index,
    })
  },
  clickItem6(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex6: data.index,
    })
  },
  clickItem61(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex61: data.index,
    })
  },
  clickItem7(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex7: data.index,
    })
  },
  clickItem8(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex8: data.index,
    })
  },
  clickItem9(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex9: data.index,
    })
  },

  clickItem10(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex10: data.index,
    })
  },
  clickItem11(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex11: data.index,
    })
  },
  clickItem12(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex12: data.index,
    })
  },
  onClickIcon() {
    wx.setStorageSync(
      'address_get_type',
      0
    );
    wx.navigateTo({ url: '/page5/addresslist/index' });
  },
  doSubmit() {
    let sData = this.data;
    console.log(sData)
    let postData = {
      // "cookOther": sData.selectIndex6 !== '' ? sData.list6[sData.selectIndex6].name : '',
      "deliveryMode": sData.selectIndex6 !== '' ? sData.list10[sData.selectIndex6].name : '',
      "edd": sData.eddDate,
      "houseStay": sData.selectIndex5 !== '' ? sData.list5[sData.selectIndex5].name : '',
      "isAllDay": sData.selectIndex4 !== '' ? sData.list4[sData.selectIndex4].name : '',
      "serviceTimeBeg": sData.selectIndex4 == 0 ? '' : sData.serviceTimeBeg,
      "serviceTimeEnd": sData.selectIndex4 == 0 ? '' : sData.serviceTimeEnd,
      "name": sData.username,
      "phone": sData.mobile,
      "position": sData.selectIndex5 !== '' ? sData.list5[sData.selectIndex5].name : '',
      // "postTime": sData.selectIndex12 !== '' ? sData.list12[sData.selectIndex12].name : '',
      "remark": sData.remark,
      "salary": sData.selectIndex9 !== '' ? sData.list9[sData.selectIndex9].name : '',
      "twins": sData.selectIndex7 !== '' ? sData.list7[sData.selectIndex7].name : '',
      "whichBirth": sData.selectIndex61 !== '' ? sData.list61[sData.selectIndex61].name : '',
      "workPlace": sData.selectIndex8 !== '' ? sData.list8[sData.selectIndex8].name : '',
      "workPlaceParticular": sData.workPlaceParticular,
      "storesId": sData.storesItem.id,
      "storesName": sData.storesItem.name,
      "storesAddress": sData.storesItem.address,
    };
    if (postData.name == '' || postData.name == null) {
      wx.showModal({
        title: '温馨提示',
        content: '姓名不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (!/^1[3456789]\d{9}$/.test(postData.phone)) {
      console.log('电话号码不正确！')
      wx.showModal({
        title: '温馨提示',
        content: '电话号码不正确！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (postData.edd == '' || postData.edd == null) {
      wx.showModal({
        title: '温馨提示',
        content: '预产日期不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }

    let content = ''
    // sData.selectIndex12 === '' ? content = '期望到岗时间' : null;
    sData.selectIndex10 === '' ? content = '您的分娩方式' : null;
    sData.selectIndex8 === '' ? content = '期望服务地区' : null;
    sData.selectIndex9 === '' ? content = '您选择月嫂的级别' : null;
    sData.selectIndex7 === '' ? content = '目前是单胎还是双胎' : null;
    sData.selectIndex61 === '' ? content = '目前是第几胎' : null;
    // sData.selectIndex6 === '' ? content = '您是否需要月嫂做其他家人饭菜' : null;
    sData.selectIndex5 === '' ? content = '月嫂的休息区域' : null;
    sData.selectIndex4 === '' ? content = '月嫂工作时间' : null;

    if (content) {
      wx.showModal({
        title: '温馨提示',
        content: '请选择' + content,
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (postData.storesId == '' || postData.storesId == null) {
      wx.showModal({
        title: '温馨提示',
        content: '门店不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (postData.workPlace == '' || postData.workPlace == null) {
      console.log('地址不能为空！')
      wx.showModal({
        title: '温馨提示',
        content: '地址不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }

    util.https('obstetricNursing/obstetricNursingAdd', postData, (res) => {
      console.log(res)
      wx.redirectTo({
        url: '/page2/result/index',
      })
    }, (res) => {
      console.log(res)
      wx.showModal({
        title: '温馨提示',
        content: res.errorMsg,
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getStoresInit();
    let currentDate = new Date(), currentDate2 = new Date();
    const year = currentDate.getFullYear();
    currentDate.setFullYear(year - 3);
    currentDate2.setFullYear(year + 3);
    this.setData({
      mindate: currentDate.getTime(),
      maxdate: currentDate2.getTime(),
    })

    wx.getStorage({
      key: 'address_get',
      success: (res) => {
        console.log(res)
        if (res.data) {
          let resData = res.data;
          this.setData({
            workPlaceParticular: resData.addrLine1 + "，" + resData.addrLine2,
          })
        } else {
          this.setData({
            workPlaceParticular: '',
          })
        }
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  
})